<?php
class Center extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        if (file_exists('application/controllers/install.php'))
            redirect('install');
        $this->load->library('session');
        $this->load->model('user_model');

        if (!($this->session->userdata['login'])) {
            redirect('poster_admin/login');
        }
    }

    function index() {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('oldpass', '原密码', 'trim|required|md5');
        $this->form_validation->set_rules('newpass', '新的密码', 'trim|required|min_length[6]|max_length[20]|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', '确认密码', 'trim|required');

        $data['title'] = '个人中心';
        $data['error'] = '';
        $data['username'] = $this->session->userdata('username');   //Get the username through session

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/center', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        } else {
            if ($this->user_model->changePassword($data['username'])) {
                $data['error'] = '修改成功';
            } else {
                $data['error'] = '原密码错误';
            }
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/center', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        }

    }
}